package cn.tedu.javagame.ball;

import java.awt.*;
import java.util.Random;

public class Ball {
    int x,y;
    int length;
    int speed;
    Color color;
    int state = 0;//0左上角  1右上角  2左下角 3右下角
    // 定义红、黄、蓝、绿、紫这几种颜色
    Color[] colors = {
            Color.RED,
            Color.YELLOW,
            Color.BLUE,
            Color.GREEN,
            new Color(128, 0, 128) // 紫色
    };
    Random random = new Random();

    public Ball(){
        this.length=30;
        this.x=(int)(Math.random()*300-length);
        this.y=(int)(Math.random()*BallFrame.HEIGHT-length);
        this.speed = 4;
        // 随机选择一种颜色
        int randomIndex = (int) (Math.random() * colors.length);
        this.color = colors[randomIndex];
    }


    // 增加球速度的方法
    public void increaseSpeed() {
            this.speed++;
    }
    public int getSpeed() {
        if (this.speed > 10) {
            this.speed = 10;
        }
        return this.speed;
    }

    public void move(){
        //根据状态更改坐标
        if(this.state==0){
            this.x -= this.speed;
            this.y -= this.speed;
        }
        if(this.state==1){
            this.x += this.speed;
            this.y -= this.speed;
        }
        if(this.state==2){
            this.x -= this.speed;
            this.y += this.speed;
        }
        if(this.state==3){
            this.x += this.speed;
            this.y += this.speed;
        }
        //撞到边界，根据状态来改状态
        if(this.x<=0){//上边界
            if(this.state==0){this.state=1;}
            if(this.state==2){this.state=3;}
        }
        if(this.y<=0){//左边界
            if(this.state==0){this.state=2;}
            if(this.state==1){this.state=3;}
        }
        if(this.x+this.length>=BallFrame.WIDTH){//右边界
            if(this.state==1){this.state=0;}
            if(this.state==3){this.state=2;}
        }
        if(this.y+this.length>=BallFrame.HEIGHT){//下边界
            if(this.state==2){this.state=0;}
            if(this.state==3){this.state=1;}
        }
    }
    public boolean hitBrick(Brick brick){
        //碰撞检测
        if ((this.x + this.length >= brick.x) && (this.x <= brick.x + brick.width) &&
                (this.y + this.length >= brick.y) && (this.y <= brick.y + brick.height)) {
            // 处理球与方块碰撞后的状态改变，不改变颜色
            if (this.state == 0) {
                if (this.y + this.length >= brick.y) {
                    this.state = 2;
                } else if (this.x + this.length >= brick.x) {
                    this.state = 1;
                }
            } else if (this.state == 1) {
                if (this.y + this.length >= brick.y) {
                    this.state = 3;
                } else if (this.x <= brick.x + brick.width) {
                    this.state = 0;
                }
            } else if (this.state == 2) {
                if (this.y <= brick.y + brick.height) {
                    this.state = 0;
                } else if (this.x + this.length >= brick.x) {
                    this.state = 3;
                }
            } else if (this.state == 3) {
                if (this.y <= brick.y + brick.height) {
                    this.state = 1;
                } else if (this.x <= brick.x + brick.width) {
                    this.state = 2;
                }
            }
//            this.changeColor(); // 调用 changeColor 方法改变球的颜色
            return true;
        }
        return false;
    }


    // 随机改变球的颜色
    public void changeColor() {
        int index = random.nextInt(colors.length);
        color = colors[index];
    }
}
